package com.woniuxy.store.core.service.impl;

import cn.hutool.core.io.FileUtil;
import com.woniuxy.common.ApiException;
import com.woniuxy.common.ResultCode;

import com.woniuxy.store.core.mapper.MdStorefirstMapper;
import com.woniuxy.store.core.mapper.MdStoresecondMapper;
import com.woniuxy.store.core.mapper.MdStorethirdMapper;
import com.woniuxy.store.core.model.MdStore;
import com.woniuxy.store.core.mapper.MdStoreMapper;
import com.woniuxy.store.core.model.MdStoresecond;
import com.woniuxy.store.core.model.MdStorethird;
import com.woniuxy.store.core.service.MdStorefirstService;
import com.woniuxy.store.core.vo.MdStoreVo;
import com.woniuxy.store.core.model.MdStorefirst;
import com.woniuxy.store.core.service.MdStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.store.core.vo.MdStorefirstVo;
import com.woniuxy.store.core.vo.MdStoresecondVo;
import com.woniuxy.store.core.vo.MdStorethirdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gmx
 * @since 2021-06-29
 */
@Service
public class MdStoreServiceImpl extends ServiceImpl<MdStoreMapper, MdStore> implements MdStoreService {

    @Autowired(required = false)
    MdStoreMapper mdStoreMapper;

    @Autowired(required = false)
    MdStorefirstService mdStorefirstService;




    @Value("${file.path}")
    private String filePath;

//未写:通过storeId查三级列表
    @Override
    public MdStoreVo selectOnestore(int storeId) {
        MdStoreVo mdStoreVo=new MdStoreVo();
        MdStorefirstVo mdStorefirstVo=new MdStorefirstVo();
        MdStore mdStore= mdStoreMapper.selectById(storeId);
        List<MdStorefirstVo> listfirstvo =mdStorefirstService.listfirstvo(storeId);
        System.out.println("list"+listfirstvo);
        mdStoreVo.setMdStore(mdStore);
        mdStoreVo.setList(listfirstvo);
        return mdStoreVo;
    }

    @Override
    public void insertOnestore(MdStore mdStore) {
        mdStore.setStoreId(null);
        mdStoreMapper.insert(mdStore);
    }

    @Override
    public void delectOnestore(int storeId) {
        mdStoreMapper.delectOnestore(storeId);
    }

    @Override
    public int application(MdStore mdStore) {
        mdStore.setStorePic("../../../static/"+mdStore.getStorePic());
        mdStore.setStoreStatus(0);
        return mdStoreMapper.insert(mdStore);
    }

    @Override
    public int check(Integer storeId) {
        return mdStoreMapper.check(storeId);
    }

    @Override
    public List<MdStore> queryAllByLimit(String storeName) {
        return mdStoreMapper.queryAllByLimit(storeName);
    }


    @Override
    public String upload(MultipartFile file) {
        //获取文件的扩展名
        String ext = FileUtil.extName(file.getOriginalFilename());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String rename = sdf.format(new Date());
        //文件名
        rename = rename.concat(".").concat(ext);

        try {
            if(!FileUtil.exist(filePath)){
                FileUtil.mkdir(filePath);
            }
            //文件归档
            file.transferTo(new File(filePath.concat(rename)));
            return rename;
        } catch (IOException e) {
            // e.printStackTrace();
            log.error("文件上传失败",e);
            //异常转译
            throw new ApiException(ResultCode.FILE_UPLOAD_FARL);
        }
    }

    @Override
    public MdStore selectByStoreName(String storeName) {
        return mdStoreMapper.selectByStoreName(storeName);
    }

}
